﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class Vote : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "Vote";
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }


        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Thăm dò ý kiến");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }

    
        public override Portal.Interface.WebPartInt[] SubWebParts
        {
            get
            {
                Portal.Interface.WebPartInt[] SubWebParts = new Portal.Interface.WebPartInt[]{
                    new AddVote(),new UpdateVote()
                };
                return SubWebParts;
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string belongSite)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                Environments.CheckWebPartAllow(WebPartId, true);
                string SelectSite = ResourceBLL.GetText(WebPartId, "SelectSite", "Chọn site") + ": " +
                       "<select id=drpSelectSiteForVote onchange=javascript:RunFilter();>";
                string[] Sites = Environments.GetSites();
                for (int iIndex = 0; iIndex < Sites.Length; iIndex++)
                {
                    if (Sites[iIndex].ToLower().Equals(belongSite))
                    {
                        SelectSite += "<option selected value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                    else
                    {
                        SelectSite += "<option value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                }
                SelectSite += "</select>";
                DataTable dtVote = new VoteBLL().Reading(belongSite);
                string RetHtml =
                    "  <div>" + SelectSite + "<input type=button onclick=\"javascript:ClientReadingVote();\" value=\"" + ResourceBLL.GetText(WebPartId, "RefreshButton", "Lọc dữ liệu") + "\"><input type=button onclick=\"javascript:ClientAddVote();\" value=\"" + ResourceBLL.GetText(WebPartId, "AddButton", "Thêm mới") + "\"></div>" +
                    "  <div id=divProcessing></div>";
                if (dtVote.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=
                    "   <table>" +
                    "       <tr>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "TH_Subject", "Tiêu đề") + "</th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "       </tr>";


                    for (int iIndex = 0; iIndex < dtVote.Rows.Count; iIndex++)
                    {
                        string VoteId = (string)dtVote.Rows[iIndex]["VoteId"];
                        string Subject = (string)dtVote.Rows[iIndex][Environments.ActiveLanguage + "Subject"];
                        string DeleteUrl = "javascript:ClientDeleteVote('" + VoteId + "')";
                        string SyncUrl = "javascript:ClientSyncWithServer('" + VoteId + "')";

                        RetHtml +=
                        "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                        "           <td>" + Subject + "</td>" +
                        "           <td><a href=\"javascript:ShowClientUpdateUrlForm('" + VoteId + "');\">" + ResourceBLL.GetText(WebPartId, "TH_Update", "Cập nhật") + "</a></td>" +
                        "           <td><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Delete", "Xóa") + "</a></td>" +
                        "           <td><a href=\"" + SyncUrl + "\">" + ResourceBLL.GetText(WebPartId, "SyncItem", "Sync") + "</a></td>" +
                        "       </tr>";
                    }
                    RetHtml +=
                        "   </table>";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CMS.Vote));
            string ActiveSite = Portal.Environment.Environments.GetActiveSite();
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "Vote.js") +
                "<div id=divVoteMain class=cssDivVote>" + GetHtml(ActiveSite) + "</div>";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetAddNewUrl(string belongSite)
        {
            return Portal.Environment.Screen.BuildDialogUrl(new ParamCls[]{
                new ParamCls("WebPartId",new AddVote().WebPartId),
                new ParamCls("BelongSite",belongSite)
            });
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId,"ConfirmDeleteMsg","Bạn đã chắc chắn xóa chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteVote(string VoteId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                
                SqlTransaction sqlTransaction = Environment.Environments.CreateTransaction();
                try
                {
                    new VoteBLL().DeleteVote(sqlTransaction, VoteId);
                    sqlTransaction.Commit();
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    throw (ex);
                }
                return ResourceBLL.GetText(WebPartId,"DeleteOkMessage","Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string VoteId)
        {
            string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateVote().WebPartId),
                        new ParamCls("VoteId",VoteId)
                    });
            return UpdateUrl;
        }


        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            return ResourceBLL.GetText(WebPartId, "Processing", "Đang đọc dữ liệu...");
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideSyncWithServer(string VoteId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                new VoteBLL().SyncWithServer(VoteId);
                return GetText("SyncOk", "Đồng bộ với máy chủ web thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncAllMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncAllMsg", "Bạn đã chắc chắn update lại dữ liệu tin tức trên máy chủ từ máy local chưa?");
        }
    }
}
